package com.winit.openapi.tools.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.outbound.OrderBuyerHouseNoSplitService;
import com.winit.oms.spi.outbound.command.order.OutboundOrderHouseNoSplitCommand;
import com.winit.oms.spi.vo.v2.order.BuyerInfo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.gfs.controller.GfsOrderController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.pis.spi.limit.ServiceLimitValidatorService;
import com.winit.pis.spi.limit.command.ServiceTypeLimitCommand;
import com.winit.pis.spi.vo.AddressVo;
import com.winit.pis.spi.vo.ServiceTypeValidateVo;
import com.winit.pis.spi.vo.ValidateMessage;

/**
 * 
 * 德国地址校验
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * zejun.zhou	1.0  	2016-12-1 	Created
 *
 * </pre>
 * @since 1.
 */

@Controller
@RequestMapping(value = "/winit/tools/address")
public class DhlAddressCheckController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(GfsOrderController.class);

    @Resource
    private ServiceLimitValidatorService serviceLimitValidatorService;
    
    @Resource
    private OrderBuyerHouseNoSplitService orderBuyerHouseNoSplitService;

    /**
     * 德国地址校验
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/isValid")
    public String isValid(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            
            OutboundOrderHouseNoSplitCommand outboundOrderHouseNoSplitCommand = new OutboundOrderHouseNoSplitCommand();
            BuyerInfo iBuyerInfo = new BuyerInfo();
            iBuyerInfo.setBuyerAddress1(json.getString("street"));
            iBuyerInfo.setBuyerHouseNo(json.getString("houseNo"));
            iBuyerInfo.setBuyerCountryCode(json.getString("country"));
            outboundOrderHouseNoSplitCommand.setBuyerInfo(iBuyerInfo);
            logger.info("isValid input oms {}" ,JSON.toJSONString(outboundOrderHouseNoSplitCommand));
            BuyerInfo obuyerInfo = orderBuyerHouseNoSplitService.buyerInfoHouseNoSplit(outboundOrderHouseNoSplitCommand);
            logger.info("isValid output oms {}" ,JSON.toJSONString(obuyerInfo));
            AddressVo receiverAddress = new AddressVo();
            receiverAddress.setCity(json.getString("city"));
            receiverAddress.setPostcode(json.getString("zipcode"));
            receiverAddress.setHouseNumber(obuyerInfo.getBuyerHouseNo());
            receiverAddress.setAddress1(obuyerInfo.getBuyerAddress1());
            receiverAddress.setCountryCode(json.getString("country"));
            
            ServiceTypeLimitCommand command = new ServiceTypeLimitCommand();
            ServiceTypeValidateVo vo = new ServiceTypeValidateVo();
            vo.setReceiverAddress(receiverAddress);
            command.setServiceTypeValidateVo(vo);
            List<ValidateMessage> list = serviceLimitValidatorService.validateDhlAddressLimited(command);
            if(list!=null && list.size()>0){
            	ValidateMessage message = list.get(0);
                responseMsg.setData(message.getData());
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
}
